package cz.uvn.orthopedy.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

import cz.uvn.orthopedy.client.ui.LoginPanel;
import cz.uvn.orthopedy.client.ui.UIUtils;

public class OperationJournal implements EntryPoint {

	private ApplicationController controller;

	public OperationJournal() {
		// controller = new ApplicationController();
	}

	public void onModuleLoad() {
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(),
				new AsyncCallback<LoginInfo>() {
					public void onFailure(Throwable error) {
						UIUtils.handleError(error);
					}

					public void onSuccess(LoginInfo loginInfo) {
						if (loginInfo.isLoggedIn()) {
							loadJournal(loginInfo);
						} else {
							loadLogin(loginInfo);
						}
					}
				});
	}

	protected void loadLogin(LoginInfo loginInfo) {
		RootPanel.get("journal").add(new LoginPanel(loginInfo));
	}

	private void loadJournal(LoginInfo loginInfo) {
		controller = new ApplicationController(loginInfo);
		RootPanel.get("menu").add(controller.getMenuPanel());
		RootPanel.get("journal").add(controller.getMainPanel());
        RootPanel.get("header").add(controller.getHeaderPanel());

		controller.showJournal(false);
	}
}
